<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;

class Orders extends ModelBase
{
    /**
     * 数据表名
     * @var string
     */
    protected $table = 'orders';

    /**
     * 订单状态
     */
    const PAY_STATUS_CREATE       = 0; // 创建订单
    const PAY_STATUS_PAY_OK       = 1; // 支付成功
    const PAY_STATUS_CALLBACK_OK  = 2; // 发货成功
    const PAY_STATUS_CALLBACK_ERR = 3; // 发货失败
    const PAY_STATUS_RUNING       = 11; // 支付进行中
    const PAY_STATUS_ERROR        = 12; // 支付金额异常
    const PAY_STATUS_XUNI         = 201; // 虚拟订单

    /**
     * 商品类型
     */
    const GOODS_TYPE_NORMAL = 1; // 普通商品
    const GOODS_TYPE_DAIJINJUAN = 2; // 代金券
    const GOODS_TYPE_XUNI = 3; // 虚拟商品

    /**
     * 支付类型
     */
    const PAY_PLATFORM_ALIPAY_H5        = 11;   //支付宝-H5
    const PAY_PLATFORM_ALIPAY_QR        = 12;   //支付宝-二维码
    const PAY_PLATFORM_WECHAT_H5        = 21;   //微信支付-H5
    const PAY_PLATFORM_WECHAT_JSSDK     = 22;   //微信支付-公众号支付
    const PAY_PLATFORM_WECHAT_NAVTIVE   = 23;   //微信支付-扫码支付
    const PAY_PLATFORM_WECHAT_MIDAS     = 24;   //微信支付-米大师
    const PAY_PLATFORM_EPAY             = 31;   //易宝支付-银联
    const PAY_PLATFORM_IAP              = 41;   //苹果内购
    const PAY_PLATFORM_POINT            = 51;   //平台币
    const PAY_PLATFORM_GOOGLE           = 61;   //google内购
    const PAY_PLATFORM_PAYSSION_H5      = 71;   //payssion-H5
    const PAY_PLATFORM_COIN             = 81;   //平台币支付
    const PAY_PLATFORM_OPPO             = 91;   //OPPO支付
    const PAY_PLATFORM_OPPO_DJ          = 92;   //OPPO单机
    const PAY_PLATFORM_NL_CHECKOUT      = 101;  //越南第三方NL-Checkout支付
    const PAY_PLATFORM_NL_CARD          = 111;  //越南第三方NL-Card支付
    const PAY_PLATFORM_BANK             = 121;  //线下银行卡转账
    const PAY_PLATFORM_ONE_STORE        = 131;  //OneStore
    const PAY_PLATFORM_YIBAO_WECHAT_H5  = 141;  //易宝微信H5
    const PAY_PLATFORM_WAN_WECHAT_JSSDK = 151;  //微信支付-公众号支付

    /**
     * NganLuong支付方式
     */
    const NL_PAY_CHECKOUT_NL                  = 'NL';
    const NL_PAY_CHECKOUT_VISA                = 'VISA';
    const NL_PAY_CHECKOUT_ATM_ONLINE          = 'ATM_ONLINE';
    const NL_PAY_CHECKOUT_ATM_OFFLINE         = 'ATM_OFFLINE';
    const NL_PAY_CHECKOUT_NH_OFFLINE          = 'NH_OFFLINE';
    const NL_PAY_CHECKOUT_IB_ONLINE           = 'IB_ONLINE';
    const NL_PAY_CHECKOUT_CREDIT_CARD_PREPAID = 'CREDIT_CARD_PREPAID';
    const NL_PAY_CHECKOUT_QRCODE              = 'QRCODE';
    const NL_PAY_CHECKOUT_CASH_IN_SHOP        = 'CASH_IN_SHOP';

    /**
     * 根据支付类型，返回支付操作类名
     * @return array
     */
    public static function getPayClassList()
    {
        $list = [
            self::PAY_PLATFORM_ALIPAY_H5       => 'AlipayH5',
            self::PAY_PLATFORM_ALIPAY_QR       => 'AlipayQr',
            self::PAY_PLATFORM_WECHAT_H5       => 'WxpayH5',
            self::PAY_PLATFORM_WECHAT_JSSDK    => 'WxpayJssdk',
            self::PAY_PLATFORM_WECHAT_NAVTIVE  => 'WxpayNavtive',
            self::PAY_PLATFORM_EPAY            => 'Epay',
            self::PAY_PLATFORM_IAP             => 'IOSIap',
            self::PAY_PLATFORM_POINT           => 'Point',
            self::PAY_PLATFORM_GOOGLE          => 'GooglePay',
            self::PAY_PLATFORM_PAYSSION_H5     => 'PayssionH5',
            self::PAY_PLATFORM_COIN            => 'PlatformCoin',
            self::PAY_PLATFORM_OPPO            => 'OppoPay',
            self::PAY_PLATFORM_OPPO_DJ         => 'OppoDjPay',
            self::PAY_PLATFORM_NL_CHECKOUT     => 'NLCheckoutPay',
            self::PAY_PLATFORM_NL_CARD         => 'NLCardPay',
            self::PAY_PLATFORM_BANK            => 'BANK',
            self::PAY_PLATFORM_ONE_STORE       => 'OneStorePay',
            self::PAY_PLATFORM_YIBAO_WECHAT_H5 => 'YibaoWxH5',
            self::PAY_PLATFORM_WAN_WECHAT_JSSDK => 'WanSdkWxpayJssdk',
        ];
        return $list;
    }

    /**
     * 根据pay_platform获取对应的处理类
     * @param $pay_platform
     * @return mixed
     */
    public static function getSdkClassName($pay_platform)
    {
        $list = self::getPayClassList();
        return $list[$pay_platform];
    }

    /**
     * 乐观锁存储
     * @param array $update 更新内容
     * @return void
     */
    public function saveByKey($update)
    {
        $trade_no               = $this->trade_no;
        $lock_version           = $this->lock_version;
        $update['lock_version'] = $lock_version + 1;
        DB::table($this->table)->where('trade_no', $trade_no)->where('lock_version', $lock_version)->update($update);
    }
}
